load("elixir_home.bzl", "elixir_home")
load(":rabbitmq_home.bzl", "rabbitmq_home")
load(":rabbitmq_run.bzl", "rabbitmq_run", "rabbitmq_run_command")
load(":rabbitmqctl.bzl", "rabbitmqctl")
load(":rabbitmq.bzl", "management_plugins", "required_plugins")

exports_files([
    "scripts/bazel/rabbitmq-run.sh",
])

# This allows us to
# `bazel build //my/target \
#    --//:elixir_home=/path/to/elixir/installation`
elixir_home(
    name = "elixir_home",
    build_setting_default = "~/.kiex/elixirs/elixir-1.10.4",
    visibility = ["//visibility:public"],
)

platform(
    name = "erlang_22_platform",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
        "@bazel_tools//tools/cpp:clang",
    ],
    exec_properties = {
        "OSFamily": "Linux",
        "container-image": "docker://pivotalrabbitmq/rabbitmq-server-buildenv:linux-erlang-22.3",
    },
)

platform(
    name = "erlang_23_platform",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
        "@bazel_tools//tools/cpp:clang",
    ],
    exec_properties = {
        "OSFamily": "Linux",
        "container-image": "docker://pivotalrabbitmq/rabbitmq-server-buildenv:linux-erlang-23.2",
    },
)

rabbitmq_home(
    name = "broker-home",
    plugins = required_plugins(rabbitmq_workspace = "@"),
)

rabbitmq_home(
    name = "broker-management-home",
    plugins = required_plugins(rabbitmq_workspace = "@") + management_plugins(rabbitmq_workspace = "@"),
)

rabbitmq_home(
    name = "broker-for-tests-home",
    testonly = True,
    plugins = [
        "//deps/rabbit:test_bazel_erlang_lib",  # <- compiled with test erlc_opts
        "@rabbitmq_ct_client_helpers//:bazel_erlang_lib",
        "@inet_tcp_proxy//:bazel_erlang_lib",
        "@meck//:bazel_erlang_lib",
    ],
)

rabbitmq_home(
    name = "broker-for-cli-tests-home",
    plugins = required_plugins(rabbitmq_workspace = "@") + [
        "//deps/rabbitmq_federation:bazel_erlang_lib",
        "//deps/rabbitmq_stomp:bazel_erlang_lib",
        "//deps/amqp_client:bazel_erlang_lib",
    ],
)

rabbitmq_run(
    name = "rabbitmq-run",
    home = ":broker-home",
    visibility = ["//visibility:public"],
)

rabbitmq_run(
    name = "rabbitmq-management-run",
    home = ":broker-management-home",
    visibility = ["//visibility:public"],
)

rabbitmq_run(
    name = "rabbitmq-for-tests-run",
    testonly = True,
    home = ":broker-for-tests-home",
    visibility = ["//visibility:public"],
)

rabbitmq_run(
    name = "rabbitmq-for-cli-tests-run",
    testonly = True,
    home = ":broker-for-cli-tests-home",
    visibility = ["//visibility:public"],
)

# Allow us to `bazel run broker`
# for the equivalent of `make run-broker`
# (though it as of yet includes no plugins)
rabbitmq_run_command(
    name = "broker",
    rabbitmq_run = ":rabbitmq-run",
    subcommand = "run-broker",
)

# `bazel run broker-management` for the broker with just the
# management plugin
rabbitmq_run_command(
    name = "broker-management",
    rabbitmq_run = ":rabbitmq-management-run",
    subcommand = "run-broker",
)

# `bazel run rabbitmqctl`
rabbitmqctl(
    name = "rabbitmqctl",
    home = ":broker-home",
    visibility = ["//visibility:public"],
)
